package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapBookDO;
import com.cskaoyan.wordmemorize.dao.entity.VocChapterDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapBookMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocChapterMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteChapterOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapterDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocChapterCommand;
import com.cskaoyan.wordmemorize.request.VocChapterPageRequest;
import com.cskaoyan.wordmemorize.service.VocChapterService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.List;

import static com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant.*;

@Service
public class VocChapterServiceImpl implements VocChapterService {

    @Autowired
    VocChapterMapper vocChapterMapper;

    @Autowired
    VocChapBookMapper vocChapBookMapper;

    @Autowired
    VocChapterConverter vocChapterConverter;

    @Autowired
    VocBookMapper vocBookMapper;


    // 分页查询章节列表(后端-物料管理-章节管理)
    @Override
    public PageDTO<VocChapterDTO> getChapterPage(VocChapterPageRequest pageQuery) {

        // 检查分页参数,如果没有传入分页参数,则查询所有的章节
        if (pageQuery.getPageNum() == null) {
            // 根据书籍名字，查询所有的章节
            LambdaQueryWrapper<VocChapterDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VocChapterDO::getBookId, pageQuery.getBookId());
            List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectList(queryWrapper);
            PageDTO<VocChapterDTO> vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(vocChapterDOS, (long) vocChapterDOS.size());
            return vocChapterDTOPageDTO;
        }

        // 构造查询条件
        LambdaQueryWrapper<VocChapterDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocChapterDO::getBookId, pageQuery.getBookId())
                .like(pageQuery.getKeyword() != null, VocChapterDO::getChapterName, pageQuery.getKeyword());

        // 构造分页对象
        Page<VocChapterDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());

        // 执行查询
        Page<VocChapterDO> resultPage = vocChapterMapper.selectPage(page, queryWrapper);

        List<VocChapterDO> records = resultPage.getRecords();
        long total = resultPage.getTotal();

        PageDTO<VocChapterDTO> vocChapterDTOPageDTO = vocChapterConverter.vocChapterDOs2Page(records, total);
        return vocChapterDTOPageDTO;
    }

    // 添加章节(后端-物料管理-章节管理)
    @OperateLog(dataType = "章节管理", operateType = OPERATE_ADD)
    @Override
    public void addChapter(VocChapterCommand command) {
        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        vocChapterMapper.insert(vocChapterDO);

        // todo:相关书本的章节数要+1
        // 通过书本Id锁定VocBookDO中的书本，然后让其章节数+1
        LambdaUpdateWrapper<VocBookDO> vocChapterDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        vocChapterDOLambdaUpdateWrapper
                .eq(VocBookDO::getId, vocChapterDO.getBookId())
                .setSql("chapter_count = chapter_count + 1");

        // 用映射器更新对应书籍的章节数
        vocBookMapper.update(null, vocChapterDOLambdaUpdateWrapper);
    }


    // 修改章节(后端-物料管理-章节管理)
    @OperateLog(dataType = "章节管理", operateType = OPERATE_UPDATE)
    @Override
    public void updateChapter(VocChapterCommand command) {
        VocChapterDO vocChapterDO = vocChapterConverter.vocChapterCommand2DO(command);
        vocChapterDO.setId(Long.parseLong(command.getId()));
        vocChapterMapper.updateById(vocChapterDO);
    }

    // 删除章节(后端-物料管理-章节管理)
    @OperateLog(dataType = "章节管理", operateType = OPERATE_SINGLE_DELETE)
    @Override
    public DeleteChapterOperateDTO deleteChapter(Long chapterId) {
        VocChapterDO vocChapterDO = vocChapterMapper.selectById(chapterId);
        DeleteChapterOperateDTO deleteChapterOperateDTO = vocChapterConverter.vocChapterDO2DeleteDTO(vocChapterDO);
        System.out.println("deleteChapterOperateDTO = " + deleteChapterOperateDTO);

        // todo:删除voc_chap_book中数据
        LambdaQueryWrapper<VocChapBookDO> vocChapBookQueryWrapper = new LambdaQueryWrapper<>();
        vocChapBookQueryWrapper.eq(VocChapBookDO::getChapterId, chapterId);
        vocChapBookMapper.delete(vocChapBookQueryWrapper);

        // todo:删除 voc_chapter中数据
        vocChapterMapper.deleteById(chapterId);

        // todo:相关书本的章节数要-1
        // 通过章节Id锁定VocBookDO中的书本，然后让其章节数-1
        LambdaUpdateWrapper<VocBookDO> vocChapterDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        vocChapterDOLambdaUpdateWrapper
                .eq(VocBookDO::getId, vocChapterDO.getBookId())
                .setSql("chapter_count = chapter_count -1");
        // 用映射器更新对应书籍的章节数
        vocBookMapper.update(null, vocChapterDOLambdaUpdateWrapper);

        return deleteChapterOperateDTO;
    }


    // 批量删除选中章节(后端-物料管理-章节管理)
    @OperateLog(dataType = "章节管理", operateType = OPERATE_BATCH_DELETE)
    @Override
    public List<DeleteChapterOperateDTO> deleteChapters(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        List<VocChapterDO> vocChapterDOS = vocChapterMapper.selectByIds(ids);
        List<DeleteChapterOperateDTO> deleteChapterOperateDTOS = vocChapterConverter.vocChapterDOs2DeleteDTOs(vocChapterDOS);

        // todo:删除voc_chap_book中数据
        LambdaQueryWrapper<VocChapBookDO> vocChapBookQueryWrapper = new LambdaQueryWrapper<>();
        vocChapBookQueryWrapper.in(VocChapBookDO::getChapterId, ids);
        vocChapBookMapper.delete(vocChapBookQueryWrapper);

        // todo:删除 voc_chapter中数据
        vocChapterMapper.deleteByIds(ids);

        // todo：快展示了，走捷径飙车

        // n个改变
        Integer changedNumber = ids.size();

        // todo：赶时间，借老乡的人头一用，这是目标书本的Id
        Long longId = vocChapterDOS.get(0).getBookId();

        // todo:相关书本的章节数要-n
        // 通过章节Id锁定VocBookDO中的书本，然后让其章节数-n
        LambdaUpdateWrapper<VocBookDO> vocChapterDOLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        vocChapterDOLambdaUpdateWrapper
                .eq(VocBookDO::getId, longId)
                .setSql("chapter_count = chapter_count - " + changedNumber);
        // 用映射器更新对应书籍的章节数
        vocBookMapper.update(null, vocChapterDOLambdaUpdateWrapper);


        return deleteChapterOperateDTOS;


    }
}
